package com.yuqian.JavaSe.java.sreflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.Scanner;

public class ReflectionTest {
	public static void main(String[] args) {
	/*	String name;
		if(args.length>0) name = args[0];
		else{
			Scanner in = new Scanner(System.in);
			System.out.println("请输入Class名字:");
			name = in.next();
		}*/
		try{
			
			Human huanm = new Human();
			Eat run = (Eat) Proxy.newProxyInstance(Human.class.getClassLoader(), new Class[]{Run.class,Eat.class}, new ProxyHandler(huanm));
			//run.run();
			//run.eat();
			Class cl =run.getClass();
			//Class cl = Class.forName(name);
			Class supercl = cl.getSuperclass();
			String modifiers = Modifier.toString(cl.getModifiers());
			if(modifiers.length()>0)
				System.out.print(modifiers+" class "+cl.getSimpleName());
			//显示父类
			if(supercl!=null&&supercl!=Object.class)
				System.out.print(" extends "+supercl.getSimpleName());
			Class[] interfaces  = cl.getInterfaces();
			//显示实现的接口
			if(interfaces.length>0){
				System.out.print(" implements ");
				for(int i=0;i<interfaces.length;i++){
					if(i>0) System.out.print(", ");
					System.out.print(interfaces[i].getSimpleName());
				}
			}
		
			
			
			System.out.print("{");
			System.out.println("");
			printConstructors(cl);
			System.out.println("");
			printMethods(cl);
			System.out.println("");
			printFields(cl);
			System.out.println("}");
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void printConstructors(Class cl){
		Constructor[] constructors = cl.getDeclaredConstructors();
		
		for(Constructor c:constructors){
			String name = c.getName();
			name =name.substring( name.lastIndexOf(".")+1);
			String modifiers = Modifier.toString(c.getModifiers());
			
			System.out.print(modifiers+"  "+name+"(");
			Class[] parClasses = c.getParameterTypes();
			for(int i=0;i<parClasses.length;i++){
				if(i>0) System.out.print(", ");
				System.out.print(parClasses[i].getSimpleName());
			}
			System.out.print(");");
			System.out.println("");
		}
	}
	
	public static void printMethods(Class cl){
		Method[] mes = cl.getDeclaredMethods();
		for(Method m:mes){
			Class retType = m.getReturnType();
			String retName = retType.getSimpleName();
			String name = m.getName();
			
			//name =name.substring( name.lastIndexOf("."));
			String modifiers =Modifier.toString(m.getModifiers());
			System.out.print(" "+modifiers+" "+retName+" "+name+"(");
			Class[] parClasses = m.getParameterTypes();
			for(int i=0;i<parClasses.length;i++){
				if(i>0) System.out.print(", ");
				System.out.print(parClasses[i].getSimpleName());
			}
			System.out.print(");");
			System.out.println("");
		}
	}
	
	public static void printFields(Class cl){
		Field[] fls = cl.getDeclaredFields();
		for(Field f:fls){
			String modifiers =Modifier.toString(f.getModifiers());
			Class type =f.getClass();
			String name = f.getName();
			name =name.substring( name.lastIndexOf(".")+1);
			System.out.print(" "+modifiers+" "+type.getSimpleName()+" "+name+";");
			System.out.println("");
		}
	}
}
